"use strict";
import axios from "axios";
import {useRouter} from 'vue-router'
const router = useRouter();
import QS from 'qs';
import store from "@/store";
let config = {
    // baseURL: 'http://localhost:11434',
    baseURL: '/api',
    timeout: 1000000, // Timeout
    withCredentials: true, // Check cross-site Access-Control
};

const _axios = axios.create(config);

_axios.interceptors.request.use(
    function(response) {
        // Do something before request is sent
        console.log(response)
        const dateTime = Date.parse(new Date())
        console.log(response.url.indexOf('/login') === -1)
        const token = window.sessionStorage.getItem('token')
        if (response.url.indexOf('/login') === -1 ) {
            response.headers.Authorization= token;
        }
        if (response.method === 'get') {
            response.params = {
                ...response.params,
                dateTime: dateTime
            }
        }
        // if (response.da) {}
        return response;
    },
    function(error) {
        // Do something with request error
        return Promise.reject(error);
    }
);

// Add a response interceptor
_axios.interceptors.response.use(
    function(response) {
        // Do something with response data
        console.log(response.data)
        if (response.data.code === 500) {
            alert(response.data.message)
        }else if (response.data.code === 401) {
            alert(response.data.message)
            window.sessionStorage.clear()
            store.commit('updateUserInfo', {})
            window.location.replace('/login')
        }
        return response;
    },
    function(error) {
        // Do something with response error
        return Promise.reject(error);
    }
);



// createApp(App).use(Plugin)

export default _axios;
